/**
 * Title: AsyncHttpUtil.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-8-12 下午2:45:32
 * Version 1.0 
 */
package com.luoxudong.phonenumber.service.net.http.asynchttp;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.luoxudong.phonenumber.service.base.GlobleInfo;
import com.luoxudong.phonenumber.service.net.http.asynchttp.interfaces.ICustomJsonRequestListener;
import com.luoxudong.phonenumber.service.net.http.asynchttp.interfaces.ISimpleHttpRequestListener;
import com.luoxudong.phonenumber.service.net.http.asynchttp.interfaces.ISimpleJsonRequestListener;
import com.luoxudong.phonenumber.service.net.http.asynchttp.model.BaseResponse;
import com.luoxudong.phonenumber.service.net.http.httpclient.entity.ContentType;
import com.luoxudong.phonenumber.service.pool.constant.ThreadPoolConst;

/** 
 * ClassName: AsyncHttpUtil 
 * Description:http请求工具类 
 * Create by 罗旭东
 * Date 2013-8-12 下午2:45:32 
 */
public class AsyncHttpUtil {
	/**
	 * 
	 * @description:普通http请求
	 * @param url 请求url
	 * @param listener 请求结果监听
	 * @return void
	 * @throws
	 */
	public static void simpleHttpRequest(String url, ISimpleHttpRequestListener listener)
	{
		simpleHttpGetRequest(url, null, listener);
	}
	
	/**
	 * 
	 * @description:带参数的get请求
	 * @param url 请求url
	 * @param urlParams url参数列表
	 * @param listener 返回结果监听
	 * @return void
	 * @throws
	 */
	public static void simpleHttpGetRequest(String url, Map<String, String> urlParams, ISimpleHttpRequestListener listener)
	{
		AsyncHttpRequest httpRequest = new AsyncHttpRequest();
		RequestParams params = new RequestParams();
		
		if (urlParams != null)//url参数
		{
			for(Map.Entry<String, String> entry : urlParams.entrySet()) {
				params.put(entry.getKey(), entry.getValue());
	        }
		}
		
		AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler(listener);
		httpRequest.setThreadPoolType(ThreadPoolConst.THREAD_TYPE_SIMPLE_HTTP);
		httpRequest.get(url, params, handler);
	}
	
	/**
	 * 
	 * @description:普通form请求
	 * @param url 请求url
	 * @param listener 请求监听
	 * @return void
	 * @throws
	 */
	public static void simpleHttpFormRequest(String url, Map<String, String> urlParams, ISimpleHttpRequestListener listener)
	{
		AsyncHttpRequest httpRequest = new AsyncHttpRequest();
		RequestParams params = new RequestParams();
		
		if (urlParams != null)//url参数
		{
			for(Map.Entry<String, String> entry : urlParams.entrySet()) {
				params.put(entry.getKey(), entry.getValue());
	        }
		}
		
		AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler(listener);
		httpRequest.setThreadPoolType(ThreadPoolConst.THREAD_TYPE_SIMPLE_HTTP);
		httpRequest.post(url, params, handler);
	}
	
	/**
	 * 
	 * @description:普通json请求,返回类需要继承BaseResponse
	 * @param url 请求url
	 * @param requestInfo 请求json内容
	 * @param responseClass 返回类型
	 * @param listener 返回监听
	 * @return AsyncHttpRequest http请求对象，可用于中断请求
	 * @throws
	 */
	public static <T extends Serializable, M extends BaseResponse<M>> AsyncHttpRequest simpleJsonRequest(final String url, final T requestInfo, final Class<M> responseClass, final ISimpleJsonRequestListener<M> listener)
	{
		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("phoneModel", GlobleInfo.phoneModel);
		headerMap.put("screenScale", GlobleInfo.screenScale);
		headerMap.put("screenHeight", String.valueOf(GlobleInfo.screenHeight));
		headerMap.put("screenWidth", String.valueOf(GlobleInfo.screenWidth));
		
		if (GlobleInfo.location != null)
		{
			headerMap.put("Latitude", String.valueOf(GlobleInfo.location.getLatitude()));
			headerMap.put("Latitude", String.valueOf(GlobleInfo.location.getLatitude()));
		}
		
		AsyncHttpRequest httpRequest = new AsyncHttpRequest();
		JsonRequestParams<T> params = new JsonRequestParams<T>();
		
		JsonHttpResponseHandler<M> handler = new JsonHttpResponseHandler<M>(responseClass, listener);
		
		params.setRequestJsonObj(requestInfo);
		params.setResponseHandler(handler);
		httpRequest.setThreadPoolType(ThreadPoolConst.THREAD_TYPE_SIMPLE_HTTP);
		httpRequest.post(null, url, headerMap, params, ContentType.APPLICATION_JSON.toString(), handler);
		return httpRequest;
	}
	
	/**
	 * 
	 * @description:自定义json请求，无需继承基类，错误码需要业务层判断
	 * @param url 请求url
	 * @param requestInfo 请求json内容
	 * @param responseClass 返回类型
	 * @param listener 返回监听
	 * @return AsyncHttpRequest http请求对象，可用于中断请求
	 * @throws
	 */
	public static <T extends Serializable, M extends Serializable> AsyncHttpRequest customJsonRequest(String url, T requestInfo, Class<M> responseClass, ICustomJsonRequestListener<M> listener)
	{
		AsyncHttpRequest httpRequest = new AsyncHttpRequest();
		JsonRequestParams<T> params = new JsonRequestParams<T>();
		CustomJsonHttpResponseHandler<M> handler = new CustomJsonHttpResponseHandler<M>(responseClass, listener);
		
		params.setRequestJsonObj(requestInfo);
		params.setResponseHandler(handler);
		
		httpRequest.setThreadPoolType(ThreadPoolConst.THREAD_TYPE_SIMPLE_HTTP);
		httpRequest.post(null, url, null, params, ContentType.APPLICATION_JSON.toString(), handler);
		return httpRequest;
	}
}
